﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;

public partial class manage_channel_style_add : NetCMS.Web.UI.ManagePage
{
    public manage_channel_style_add()
    {
        this.Authority_Code = "D010";
    }
    Channel rd = new Channel();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SiteID != "0")
            {
                PageError("分站没权限操作", "javascript:history.back();", true);
            }
            copyright.InnerHtml = CopyRight;
            string ChID = Request.QueryString["ChID"];
            if (ChID != string.Empty && ChID != null)
            {
                string _dirdumm = NetCMS.Config.UIConfig.dirDumm;
                if (_dirdumm.Trim() != "") { _dirdumm = "/" + _dirdumm; }
                style_base.InnerHtml = NetCMS.Common.Public.getxmlstylelist("styleContent", _dirdumm + "/xml/cuslabeStyle/channelStyle.xml");
                style_class.InnerHtml = NetCMS.Common.Public.getxmlstylelist("styleContent1", _dirdumm + "/xml/cuslabeStyle/CHstyleclass.xml");
                style_special.InnerHtml = NetCMS.Common.Public.getxmlstylelist("DropDownList2", _dirdumm + "/xml/cuslabeStyle/CHstylespecial.xml");
                GetStyleList(this.ChannelStyle);
                string Action = Request.QueryString["Action"];
                if (Action != null && Action != string.Empty)
                {
                    string ID = Request.QueryString["id"];
                    IDataReader dr = rd.GetStyleContent(int.Parse(ID.ToString()), int.Parse(ChID.ToString()));
                    if (dr.Read())
                    {
                        GetstyleClassList((this.gClassID), 0, 0, int.Parse(dr["ClassID"].ToString()));
                        this.styleName.Text = dr["styleName"].ToString();
                        this.GstyleContent.Value = dr["styleContent"].ToString();
                        if (dr["islock"].ToString() == "1")
                        {
                            this.islock.Checked = true;
                        }
                        this.styleDescript.Text = dr["styleDescript"].ToString();
                    }
                    dr.Close();
                }
                else
                {
                    this.Authority_Code = "D010";
                    this.CheckAdminAuthority();
                    string gClassID = Request.QueryString["ClassID"];
                    int sClassID = 0;
                    if (gClassID != null && gClassID != string.Empty)
                    {
                        sClassID = int.Parse(gClassID.ToString());
                    }
                    GetstyleClassList((this.gClassID), 0, 0, sClassID);
                }
            }
            else
            {
                PageError("错误的频道参数", "javascript:history.back();", true);
            }
        }
    }

    protected void GetStyleList(DropDownList lst)
    {
        int ChID = int.Parse(Request.QueryString["ChID"]);
        IDataReader dr = rd.GetDefineStyle(ChID);
        int i = 2;
        string iStr = string.Empty;
        while (dr.Read())
        {
            ListItem it = new ListItem();
            string stxt = string.Empty;
            it.Value = "{CH$" + dr["EName"].ToString() + "}";
            if (i < 10)
            {
                iStr = ("0" + i).ToString();
            }
            it.Text = iStr + "." + dr["CName"].ToString();
            lst.Items.Add(it);
            i++;
        }
        dr.Close();
    }

    protected void GetstyleClassList(DropDownList lst, int ParentID, int Layer, int sClassID)
    {
        IDataReader dr = rd.getStyleClassList(ParentID, int.Parse(Request.QueryString["ChID"]));
        while (dr.Read())
        {
            ListItem it = new ListItem();
            string stxt = "";
            it.Value = dr["ID"].ToString();
            if (Layer > 0)
                stxt = "┝";
            for (int i = 1; i < Layer; i++)
            {
                stxt += " ┉ ";
            }
            it.Text = stxt + dr["cName"].ToString();
            if (sClassID == int.Parse(dr["id"].ToString()))
            {
                it.Selected = true;
            }
            lst.Items.Add(it);
            GetstyleClassList(lst, int.Parse(dr["ID"].ToString()), (Layer + 1), sClassID);
        }
        dr.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int ID = 0;
            if (Request.QueryString["Action"] != string.Empty && Request.QueryString["Action"] != null)
            {
                ID = int.Parse(Request.QueryString["ID"]);
            }
            int ChID = int.Parse(Request.QueryString["ChID"]);
            if (this.gClassID.SelectedValue.Trim() == string.Empty)
            {
                PageError("请先创建栏目", "javascript:history.back();", true);
            }
            int classID = int.Parse(this.gClassID.SelectedValue);
            string styleName = this.styleName.Text;
            if (rd.GetStyleRecord(styleName, ID, ChID) > 0)
            {
                PageError("样式名称重复", "javascript:history.back();", true);
            }
            string StContent = GstyleContent.Value;
            if (StContent.ToLower().IndexOf("<p>") > -1 && StContent.IndexOf("</p>") > -1)
            {
                StContent = NetCMS.Common.Input.RemovePor(StContent);
            }
            int isLock = 0;
            if (this.islock.Checked)
            {
                isLock = 1;
            }
            string styleDescript = this.styleDescript.Text;
            DateTime Creatitime = DateTime.Now;
            NetCMS.Model.styleChContent uc = new NetCMS.Model.styleChContent();
            uc.Id = ID;
            uc.ChID = ChID;
            uc.classID = classID;
            uc.styleName = styleName;
            uc.styleContent = StContent;
            uc.isLock = isLock;
            uc.styleDescript = styleDescript;
            uc.creattime = Creatitime;
            uc.SiteID = SiteID;
            if (ID != 0)
            {
                //更新
                rd.UpdateStyleContent(uc);
                PageRight("更新样式成功", "style_list.aspx?ChID=" + ChID + "&ClassID=" + classID + "", true);
            }
            else
            {
                //插入
                rd.InsertStyleContent(uc);
                PageRight("插入样式成功", "style_list.aspx?ChID=" + ChID + "&ClassID=" + classID + "", true);
            }
        }
    }
}
